<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { get } from '@/utils/request'
const router = useRouter()

// 地址类型声明
interface Address {
  name: string
  phone: string
  area: string
  address: string
}
// 订单项类型声明
interface OrderItem {
  _id: string
  shopsId?: { name?: string }
  colorx?: string
  chima?: string
  num?: number
  cart?: string
}

function goToEditAddress(): void {
  router.push('/EditAddress')
}
const address = ref<Address>({
  name: '',
  phone: '',
  area: '',
  address: '',
})
const orderList = ref<OrderItem[]>([])
function goBack(): void {
//   router.back()
    router.push('/cart')
}
const loading = ref<boolean>(true)

onMounted(async () => {
  try {
    const userId = localStorage.getItem('userId') || ''
    const localAddress = localStorage.getItem('address')
    if (localAddress) {
      const arr: any[] = JSON.parse(localAddress)
      if (Array.isArray(arr) && arr.length > 0) {
        const def = arr.find((item: any) => item.isDefault)
        if (def) {
          address.value = {
            name: def.name,
            phone: def.phone,
            area: def.area,
            address: def.address
          }
        } else {
          address.value = {
            name: '无',
            phone: '',
            area: '',
            address: '暂无默认收货地址'
          }
        }
      } else {
        address.value = {
          name: '无',
          phone: '',
          area: '',
          address: '暂无收货地址'
        }
      }
    }
    if (!localAddress) {
      const res = await get('/LXL/getaddress', { userId })
      if (res.data && res.data.length > 0) {
        const def = res.data.find((item: any) => item.isDefault)
        if (def) {
          address.value = {
            name: def.name,
            phone: def.phone,
            area: def.area,
            address: def.address
          }
        } else {
          address.value = {
            name: '无',
            phone: '',
            area: '',
            address: '暂无默认收货地址'
          }
        }
      } else {
        address.value = {
          name: '无',
          phone: '',
          area: '',
          address: '暂无收货地址'
        }
      }
    }
    const orderRes = await get('/LXL/getdingdan', { userId })
    if (orderRes.data && orderRes.data.length > 0) {
      orderList.value = orderRes.data as OrderItem[]
    } else {
      orderList.value = []
    }
  } catch (e) {
    address.value = {
      name: '无',
      phone: '',
      area: '',
      address: '获取地址失败'
    }
    orderList.value = []
  }
  loading.value = false
})
function tz(): void {
  if (orderList.value.length === 0) {
    alert('请先添加商品到购物车')
    return
  }
  localStorage.removeItem('orderList')
  router.push('/PayView')
}
</script>
<template>
    <div class="order-confirmed">
        <div class="header">
            <span class="back" @click="goBack">←</span>
            <span class="title">确认订单</span>
        </div>
        <div class="divider"></div>
        <div class="section-title">送货地址</div>
        <div class="address-card" @click="goToEditAddress">
            <div class="address-icon">
                <svg width="32" height="32" viewBox="0 0 24 24" fill="none">
                    <circle cx="12" cy="10" r="3" fill="#BDBDBD" />
                    <path
                        d="M12 2C7.03 2 3 6.03 3 11c0 5.25 7.05 10.74 7.36 10.97.39.3.89.3 1.28 0C13.95 21.74 21 16.25 21 11c0-4.97-4.03-9-9-9zm0 18.54C9.14 18.09 5 14.36 5 11c0-3.87 3.13-7 7-7s7 3.13 7 7c0 3.36-4.14 7.09-7 7.54z"
                        fill="#BDBDBD" />
                </svg>
            </div>
            <div class="address-info">
                <div class="contact-info">{{ address.name }} {{ address.phone }}</div>
                <div class="address-detail">{{ address.area }} {{ address.address }}</div>
            </div>
            <div class="address-edit">
                <svg width="20" height="20" viewBox="0 0 24 24" fill="none">
                    <path
                        d="M3 17.25V21h3.75l11.06-11.06-3.75-3.75L3 17.25zM20.71 7.04a1.003 1.003 0 0 0 0-1.42l-2.34-2.34a1.003 1.003 0 0 0-1.42 0l-1.83 1.83 3.75 3.75 1.84-1.82z"
                        fill="#BDBDBD" />
                </svg>
            </div>
        </div>
        <div class="divider"></div>
        <div class="section-title">订单列表</div>
        <div class="order-list">
            <template v-if="loading">
                <div class="order-loading">
                    <span class="loader"></span>
                    <span style="margin-top:12px;">加载中...</span>
                </div>
            </template>
            <template v-else-if="orderList.length > 0">
                <div class="order-items">
                    <div class="order-item" v-for="item in orderList" :key="item._id">
                        <div class="item-img">
                            <!-- 可根据实际情况显示图片 -->
                            <svg width="48" height="48" viewBox="0 0 24 24" fill="none">
                                <rect width="24" height="24" rx="6" fill="#F2F2F2" />
                                <path d="M8 13l2.5 3.5 3.5-4.5 4 6H6l2-5z" fill="#BDBDBD" />
                            </svg>
                        </div>
                        <div class="item-info">
                            <div class="item-name">商品名: {{ item.shopsId?.name || '未知商品' }}</div>
                            <div class="item-desc">
                                <span class="color">颜色: {{ item.colorx }}</span>
                                <span class="size">尺码: {{ item.chima }}</span>
                            </div>
                            <div class="item-price">数量: {{ item.num }}</div>
                        </div>
                        <div class="item-count">{{ item.cart }}</div>
                    </div>
                </div>
            </template>
            <template v-else>
                <div class="order-empty">还没有订单哟</div>
            </template>

        </div>
        <div class="submit-bar">
            <button class="submit-btn" @click="tz">提交订单</button>
        </div>
    </div>
</template>

<style scoped>
.order-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 120px;
    color: #bdbdbd;
    font-size: 18px;
}

.loader {
    width: 36px;
    height: 36px;
    border: 4px solid #eee;
    border-top: 4px solid #111;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: 8px;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}

.order-confirmed {
    background: #fff;
    min-height: 100vh;
    font-family: 'PingFang SC', 'Microsoft YaHei', Arial, sans-serif;
}

.header {
    display: flex;
    align-items: center;
    height: 56px;
    padding: 0 0 0 12px;
    font-size: 20px;
    font-weight: bold;
    position: relative;
    background: #fff;
}

.back {
    font-size: 24px;
    margin-right: 8px;
    cursor: pointer;
}

.title {
    flex: 1;
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    margin-right: 32px;
}

.divider {
    height: 1px;
    background: #f2f2f2;
    margin: 0 0 16px 0;
}

.section-title {
    font-size: 18px;
    font-weight: bold;
    margin: 0 0 12px 16px;
}

.address-card {
    display: flex;
    align-items: center;
    background: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.04);
    border-radius: 24px;
    padding: 16px 20px;
    margin: 0 16px 24px 16px;
}

.address-icon {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: #f2f2f2;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 12px;
}

.address-info {
    flex: 1;
}

.contact-info {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 4px;
    color: #222;
}

.address-detail {
    font-size: 14px;
    color: #666;
    line-height: 1.4;
}

.address-edit {
    margin-left: 8px;
    cursor: pointer;
}

.order-list {
    margin: 0 0 80px 0;
}

.order-item {
    display: flex;
    align-items: center;
    background: #fff;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.04);
    border-radius: 24px;
    padding: 16px 20px;
    margin: 0 16px 24px 16px;

}
.order-items {
    max-height: 100vh;
    overflow-y: auto;
}
.item-img {
    width: 72px;
    height: 72px;
    background: #f2f2f2;
    border-radius: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 16px;
}

.item-info {
    flex: 1;
}

.item-name {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 8px;
}

.item-desc {
    display: flex;
    align-items: center;
    font-size: 14px;
    color: #bdbdbd;
    margin-bottom: 8px;
}

.dot {
    width: 10px;
    height: 10px;
    background: #bdbdbd;
    border-radius: 50%;
    display: inline-block;
    margin-right: 6px;
}

.color {
    margin-right: 12px;
}

.size {
    margin-right: 12px;
}

.item-name {
    font-size: 18px;
    font-weight: bold;
    color: #222;
}

.item-price {
    font-size: 18px;
    font-weight: bold;
    color: #222;
}

.item-count {
    font-size: 18px;
    color: #bdbdbd;
    margin-left: 16px;
}

.submit-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: transparent;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 80px;
    z-index: 10;
}

.submit-btn {
    width: 90vw;
    max-width: 500px;
    height: 56px;
    background: #111;
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    border: none;
    border-radius: 28px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.08);
    cursor: pointer;
    letter-spacing: 2px;
}

.order-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #bdbdbd;
    font-size: 18px;
    margin: 40px 0 32px 0;
    min-height: 120px;
    letter-spacing: 2px;
    background: #fafbfc;
    border-radius: 16px;
}

.order-empty::before {
    content: '';
    display: block;
    width: 48px;
    height: 48px;
    margin-bottom: 12px;
    background: url('https://img.icons8.com/ios-filled/50/cccccc/open-box.png') no-repeat center/contain;
    opacity: 0.5;
}
</style>